﻿#pragma once
#include"HashTable.h"

namespace myspace
{
	template<class K,class V,class Hash = OPEN_HASH::_Hash<K>>
	class unordered_map
	{
		struct UnorderedMapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename OPEN_HASH::HashTable<K, pair<K,V>, UnorderedMapKeyOfT,Hash>::iterator iterator;
		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end(); 
		}
		pair<iterator, bool> Insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);  
		}
	
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
			return ret.first->second;
		}
	private:
		OPEN_HASH::HashTable<K, pair<K, V>, UnorderedMapKeyOfT, Hash> _ht;
	};
	void TestUnorderedMap1()
	{
		cout << "TestUnorderedMap1" << endl;
		myspace::unordered_map<int, int> um;
		um.Insert(make_pair(1, 1));
		um.Insert(make_pair(3, 3));
		um.Insert(make_pair(10, 10));
		um.Insert(make_pair(5, 5));
		um.Insert(make_pair(6, 6));
		um[10] = 100;
		um[100] = 1001;

		unordered_map<int, int>::iterator it = um.begin();
		while (it != um.end())
		{
			cout << it->first << ":" << it->second << " ";
			++it;
		}
		cout << endl;
		cout << "TestUnorderedMap1" << endl;
	}

}
